{% extends "admin/change_form.html" %}
{% load i18n %}
{#{% load compressed %}#}
{% load i18n static admin_modify %}
{% load admin_urls %}
{% block extrastyle %}
    {{ block.super }}
    <link href="{% static "css/select2.min.css" %}" rel="stylesheet">
{% endblock %}

{% block extrahead %}

    {{ block.super }}

    {#<link rel="stylesheet" type="text/css" href="{{ MEDIA_URL }}h/admin/css/tree.css"/>#}

{#    {% compressed_js "jquery" %}#}
    <script>
        (function($){

            $(document).ready(function($) {

                $("#id_frequency").select2();
                $("#id_unit").select2();
                $("#id_assigned_to").select2({placeholder: '<Choose a Group>', width: 200});

                $('input#id_object_id').hide();
                $('input#id_object_id').after('<select id="generic_object_id"></select>');

                $('#generic_object_id').select2({
                    width: '350px',
                    selectOnClose: true,
                    minimumResultsForSearch: 10,
                    placeholder: "--------"
                }).on('change', function() {
                    if ($(this).val()) {
                        $('input#id_object_id').attr('value', $(this).val())
                    }
                });

                $('select#id_content_type').change(function(){
                    $('select#generic_object_id option').remove();

                    $('input#id_object_id').attr('value', '');

                    if ($(this).val()) {
                        $.get("{% url 'updatecombo' %}"+ $(this).val(), { }, function(data){
                            $('select#generic_object_id').append(data);
{#                            $("#generic_object_id").css("width","205px");#}
                        });
                    }
                });

                if ($('select#id_content_type').val()) {
                    $.get("{% url 'updatecombo' %}"+ $('select#id_content_type').val(), { },
                    function(data){
                        $('select#generic_object_id option').remove();
                        $('select#generic_object_id').append(data);

                        id = $('input#id_object_id').val();
                        $('#generic_object_id option[value='+ id +']').attr('selected', 'selected');
                    });
                }

            })

        })($);


    </script>

{% endblock %}
